package edu.hubu.talentmanagementsystem.controller.impl;

import edu.hubu.talentmanagementsystem.common.CommonResult;
import edu.hubu.talentmanagementsystem.common.RoleType;
import edu.hubu.talentmanagementsystem.controller.AbsCrudController;
import edu.hubu.talentmanagementsystem.exception.InformationNotFoundException;
import edu.hubu.talentmanagementsystem.exception.InternalServerError;
import edu.hubu.talentmanagementsystem.model.ext.Company;
import edu.hubu.talentmanagementsystem.model.ext.Staff;
import edu.hubu.talentmanagementsystem.service.ICompanyService;
import edu.hubu.talentmanagementsystem.service.IResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author moonlan
 * date 2021/3/2 下午9:50
 */
@SuppressWarnings(value = "unused")
@Api(tags = "company相关接口")
@RestController
@RequestMapping("/company")
public class CompanyController extends AbsCrudController<Company, Integer> {

    private final ICompanyService service;

    private final IResourceService resourceService;

    public CompanyController(ICompanyService service,IResourceService resourceService) {
        this.service = service;
        this.resourceService = resourceService;
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Integer.class)
    @Override
    public CommonResult selectOneById(@PathVariable Integer id, @RequestHeader("Authorization") String token) {
        Company company = service.selectById(id)
                .orElseThrow(() -> new InformationNotFoundException("Id=" + id + "的实体未找到"));
        return CommonResult.success(company);
    }

    @GetMapping("/all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("查询所有")
    @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class)
    @Override
    public CommonResult selectAllOrderBy(@RequestHeader(name = "order", required = false)  String order, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectAll(order));
    }

    @GetMapping("/page-all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("分页查询所有")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认为1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认为5", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class)
    })
    @Override
    public CommonResult selectAllPagedOrderBy(
            @RequestParam(required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize,
            @RequestHeader(name = "order", required = false)  String order, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectAllPaged(currentPage, pageSize, order));
    }

    @PostMapping("/like-page-all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("分页模糊查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认为1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认为5", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "company", value = "企业查询条件", required = true, dataTypeClass = Company.class)
    })
    @Override
    public CommonResult selectLikePagedOrderBy(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize, @RequestHeader(name = "order", required = false)  String order,
                                               @RequestBody Company company, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectLikePaged(currentPage, pageSize, company, order));
    }

    @PostMapping("/")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("保存")
    @ApiImplicitParam(name = "company", value = "企业查询条件", required = true, dataTypeClass = Company.class)
    @Override
    public CommonResult save(@RequestBody Company company, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.insert(company).orElseThrow(() -> new InternalServerError("保存失败")));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Integer.class)
    @Override
    public CommonResult deleteById(@PathVariable Integer id, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.deleteById(id));
    }

    @PutMapping("/")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID修改")
    @ApiImplicitParam(name = "company", value = "企业修改条件", required = true, dataTypeClass = Company.class)
    @Override
    public CommonResult updateById(@RequestBody Company company, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.updateById(company.getCompanyId(), company)
                .orElseThrow(() -> new InternalServerError("修改失败")));
    }

    @PatchMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    public CommonResult updatePwd(@PathVariable Integer id, String curPwd, String updatePwd) {
        return CommonResult.success(service.updatePwd(id, curPwd, updatePwd));
    }

    @GetMapping("/followCompany")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
//    @PreAuthorize("hasAnyAuthority('STAFF')")
    @ApiOperation("根据员工id查询关注公司")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffId", value = "员工编号", required = true, paramType = "query")
    })
    public CommonResult followCompany(@RequestParam Integer staffId, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectStaffFollowCompanies(staffId));
    }

    @PostMapping("/register")
    @ApiOperation("公司注册")
    @ApiImplicitParam(name = "company", value = "公司实体类", required = true, dataTypeClass = Staff.class)
    public CommonResult register(@RequestBody Company company) {
        company.setCompanyLogoId(resourceService.selectDefaultHead("company").getResourceId());
        return CommonResult.success(service.register(company));
    }

}
